package com.example.myprojectoffeixue;
/**
 * Created by ASUS on 2016/11/18.
 */

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.myprojectoffeixue.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import org.apache.http.entity.StringEntity;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author IdeasAndroid
 *         可展开（收缩）列表示例
 */
public class ListViewad extends BaseExpandableListAdapter {
    Dialog dialog;
    private Context mContext = null;
    private List<Integer> groupList;
    private List<List<CJutil>> itemList ;
    MainActivity ma;
    MyApplication m;
    public ListViewad(Context context, List<Integer>groupList, List<List<CJutil>>itemList) {
        this.mContext = context;
        this.itemList=itemList;
        this.groupList=groupList;
        ma= (MainActivity) mContext;
        m= (MyApplication) ma.getApplication();
    }

    /**
     * 初始化数据，将相关数据放到List中，方便处理
     */


    public boolean areAllItemsEnabled() {
        return false;
    }

    /*
     * 设置子节点对象，在事件处理时返回的对象，可存放一些数据
     */
    public Object getChild(int groupPosition, int childPosition) {
        return itemList.get(groupPosition).get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /*
     * 字节点视图，这里我们显示一个文本对象
     */
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder h = null;
        if (convertView == null) {
            h = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.item_list5, null);
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }

        h.l1= (LinearLayout) convertView.findViewById(R.id.l1);
        h.l2= (LinearLayout) convertView.findViewById(R.id.l2);
        h.l3= (LinearLayout) convertView.findViewById(R.id.l3);
        Log.d("date",itemList.get(groupPosition).get(childPosition)+"");
       if(itemList.get(groupPosition).size()%3==1){
            h.l1.setVisibility(View.VISIBLE);
            h.l2.setVisibility(View.INVISIBLE);
            h.l3.setVisibility(View.INVISIBLE);
        }else if(itemList.get(groupPosition).size()%3==2){
            h.l1.setVisibility(View.VISIBLE);
            h.l2.setVisibility(View.VISIBLE);
            h.l3.setVisibility(View.INVISIBLE);
        }else if(itemList.get(groupPosition).size()%3==0){
            h.l1.setVisibility(View.VISIBLE);
            h.l2.setVisibility(View.VISIBLE);
            h.l3.setVisibility(View.VISIBLE);
        }

        h.l1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // 3*childPosition+1;
                Toast.makeText(mContext,"我是第"+(3*childPosition+1),Toast.LENGTH_SHORT).show();
                Log.d("po",groupPosition+"");
                if(groupPosition==0){
                Startscen(itemList.get(groupPosition).get(3*childPosition+1).sceneId,1,3*childPosition+1);}
            }
        });

        h.l2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                Toast.makeText(mContext,"我是第"+(3*childPosition+2),Toast.LENGTH_SHORT).show();
            }
        });

        h.l3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 3*childPosition+3;
                Toast.makeText(mContext,"我是第"+(3*childPosition+3),Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }


    /*
     * 返回当前分组的字节点个数
     */
    public int getChildrenCount(int groupPosition) {
        return itemList.get(groupPosition).size()/3+1;
    }

    /*
     * 返回分组对象，用于一些数据传递，在事件处理时可直接取得和分组相关的数据
     */
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    /*
     * 分组的个数
     */
    public int getGroupCount() {
        return groupList.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /*
     * 分组视图，这里也是一个文本视图
     */
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.name_item, null);
        }
        TextView name= (TextView) convertView.findViewById(R.id.name_cj);
       if(groupPosition==0){
           name.setText("手动场景");
       }else if(groupPosition==1){
           name.setText("定时场景");
       }else if(groupPosition==2){
           name.setText("联动场景");
       }
        return convertView;
    }

    /*
     * 判断分组是否为空，本示例中数据是固定的，所以不会为空，我们返回false
     * 如果数据来自数据库，网络时，可以把判断逻辑写到这个方法中，如果为空
     * 时返回true
     */
    public boolean isEmpty() {
        return false;
    }

    /*
     * 收缩列表时要处理的东西都放这儿
     */
    public void onGroupCollapsed(int groupPosition) {

    }

    /*
     * 展开列表时要处理的东西都放这儿
     */
    public void onGroupExpanded(int groupPosition) {

    }

    /*
     * Indicates whether the child and group IDs are stable across changes to
     * the underlying data.
     */
    public boolean hasStableIds() {
        return false;
    }

    /*
     * Whether the child at the specified position is selectable.
     */
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    class ViewHolder {

        LinearLayout l1;
        LinearLayout l2;
        LinearLayout l3;

    }



    public void Startscen(int sceneId, final int ScenType, final int postion) {
        CJutil cj2 = new CJutil();
        cj2.sceneId = sceneId;
        cj2.sceneType = ScenType;
        String j = JSON.toJSONString(cj2);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.startupScene, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        ToastUtils.showToast(mContext,"网络异常");
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            JSONArray ja = jb.getJSONArray("sceneList");
                            String msg = jb.getString("msg");
                            if ("成功".equals(msg)) {

                                HttpConnection.toastLog("场景启用成功", mContext);

                                if (ScenType == 2) {
                                    m.time_cj.get(postion).sceneStatus = "2";
                                   // c2.notifyDataSetChanged();
                                } else if (ScenType == 3) {
                                    m.l_cj.get(postion).sceneStatus = "2";
                                  //  c3.notifyDataSetChanged();
                                }


                            }else{
                                HttpConnection.toastLog("服务器错误", mContext);
                            }
                        } else {
                            HttpConnection.toastLog("服务器错误", mContext);

                        }
                    }

                });

    }



    public void Stopscen(final int postion, int ScenId, final int ScenType) {

        final CJutil cj2 = new CJutil();
        cj2.sceneId = ScenId;
        cj2.sceneType = ScenType;
        String j = JSON.toJSONString(cj2);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.shutdownScene, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.toastLog("网络异常", mContext);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String msg = jb.getString("msg");
                            if ("成功".equals(msg)) {
                                HttpConnection.toastLog("场景关闭", mContext);
                                if (ScenType == 2) {
                                    m.time_cj.get(postion).sceneStatus = "1";
                                   // c2.notifyDataSetChanged();
                                } else if (ScenType == 3) {
                                    m.l_cj.get(postion).sceneStatus = "1";
                                   // c3.notifyDataSetChanged();
                                }
                            }else{
                                HttpConnection.toastLog("服务器错误", mContext);
                            }
                        } else {
                            HttpConnection.toastLog("服务器错误",mContext);

                        }
                    }

                });


    }
    public void deleteScene(final int postion, int ScenId, final int ScenType) {

        final CJutil cj2 = new CJutil();
        cj2.sceneId = ScenId;
        String j = JSON.toJSONString(cj2);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.deleteScene, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.toastLog("网络异常",mContext);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String msg = jb.getString("msg");
                            if ("成功".equals(msg)) {


                            }else{
                                HttpConnection.toastLog("服务器错误", mContext);
                            }
                        } else {
                            HttpConnection.toastLog("服务器错误", mContext);

                        }
                    }

                });


    }




    public void getSceneDetail(final int postion, final int ScenId) {

        final CJutil cj2 = new CJutil();
        cj2.sceneId = ScenId;
        String j = JSON.toJSONString(cj2);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.getSceneDetail, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.toastLog("网络异常", mContext);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String msg = jb.getString("msg");
                            if ("成功".equals(msg)) {
                                JSONObject obj=jb.getJSONObject("sceneDetail");
                                m.cj= JSON.parseObject(obj.toString(),CJutil.class);
                                if(m.cj.sceneType==3){
                                    if("010D".equals(m.cj.deviceId)) {
                                        m.cj.sceneType=4;
                                    }else{
                                        m.cj.sceneType=3;
                                    }

                                }
                                Intent it = new Intent(mContext, AddCJActivity.class);
                                m.ScenId=ScenId;
                                it.putExtra("kinds_flag", m.cj.sceneType);
                                dialog.dismiss();
                                mContext.startActivity(it);
                            }else{
                                HttpConnection.toastLog("服务器错误", mContext);
                            }
                        } else {
                            HttpConnection.toastLog("服务器错误", mContext);

                        }
                    }

                });


    }





}
